﻿using System;
using Wisdom.Core;

namespace Wisdom.Core
{
    /// <summary>
    /// 描述：对象容器配置器
    /// </summary>
    public interface IObjectContainerConfigurator : IConfigSourceConfigurator
    { }

    /// <summary>
    /// 对象容器配置器
    /// </summary>
    public class ObjectContainerConfigurator : TypeSpecifiedConfigSourceConfigurator, IObjectContainerConfigurator
    {
        private readonly bool initFromConfigFile;
        private readonly string sectionName;
        #region Ctor
        /// <summary>
        /// 初始化 <c>ObjectContainerConfigurator</c> 类对象.
        /// </summary>
        /// <param name="context">配置上下文.</param>
        /// <param name="objectContainerType">对象容器.</param>
        /// <param name="initFromConfigFile">值 <see cref="Boolean"/> 是否可以读取配置文件.</param>
        /// <param name="sectionName">配置文件结点名称. 值必须指定，当 <paramref name="initFromConfigFile"/> 参数为值.</param>
        public ObjectContainerConfigurator(IConfigSourceConfigurator context, Type objectContainerType, bool initFromConfigFile, string sectionName)
            : base(context, objectContainerType)
        {
            this.initFromConfigFile = initFromConfigFile;
            this.sectionName = sectionName;
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Configures the container.
        /// </summary>
        /// <param name="container">The configuration container.</param>
        /// <returns>The configured container.</returns>
        protected override CodeConfigSource DoConfigure(CodeConfigSource container)
        {
            container.ObjectContainer = Type;
            container.InitObjectContainerFromConfigFile = this.initFromConfigFile;
            container.ObjectContainerSectionName = this.sectionName;
            return container;
        }
        #endregion
    }
}